package com.base.hong

import android.animation.Animator
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.base.hong.databinding.ActivitySplashBinding
import com.base.hong.ui.MainActivity
import com.base.hong.ui.TestActivity
import com.base.hong.ui.base.BaseVMActivity
import com.base.hong.ui.login.LoginActivity
import com.htt.base_library.base.NoViewModel
import com.htt.base_library.util.startActivity

class SplashActivity : BaseVMActivity<NoViewModel, ActivitySplashBinding>() {

    val list = listOf(
        "lottie_1.json",
        "lottie_2.json",
        "lottie_3.json",
        "lottie_4.json"
    )

    override fun initView(intent: Intent?, savedInstanceState: Bundle?) {

        // 避免从桌面启动程序后，会重新实例化入口类的activity
        if (!this.isTaskRoot) {
            val intent = getIntent()
            if (intent != null) {
                val action = intent.action
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN == action) {
                    finish()
                    return
                }

            }
        }
        initAnimation()
    }

    private fun initAnimation() {
        mViewBinding.lottieView.setAnimation(list.random())
        mViewBinding.lottieView.playAnimation()
        mViewBinding.lottieView.addAnimatorListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator?) {
            }

            override fun onAnimationEnd(animation: Animator?) {
                startActivity<MainActivity>()
                finish()
            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationRepeat(animation: Animator?) {
            }

        })
    }
}